﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.HtmlControls;
using System.Data;
using System.IO;

public partial class AdminPortal_Controls_Tintuc_News_Info : System.Web.UI.UserControl
{
    
    TblTinTucManager _TblTinTucManager = new TblTinTucManager();
    public int NewsID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try { NewsID = Convert.ToInt32(Request.QueryString["id"].Trim()); }
            catch { NewsID = 0; }

            loadCateParent(ddlCateParent,"category");

            loadCateParent(ddlLoaiTin, "Typecategory");

            if (NewsID == 0) //Add cate
            {
                lblTitle.Text = "Thêm tin";
                Toolbox1.ShowDel = false;
                imgbtn_Delete.Enabled = false;
                img_news.Visible = imgbtn_Delete.Visible = false;
                Toolbox1.ShowRefesh = Toolbox1.ShowBack = false;
            }
            else //sua cate
            {
                imgbtn_Delete.Enabled = true;
                lblDelete.Visible = true;
                imgbtn_Delete.ImageUrl = "~/AdminPortal/images/icon/remove_16.gif";
                lblTitle.Text = "Sửa tin: " + NewsID;
                BindData(NewsID);
            }
        }
    }

    protected void BindData(int ID)
    {        
        TblTinTucEntity _TblTinTucEntity = _TblTinTucManager.SelectOne(ID);
        if (_TblTinTucEntity == null) { lblthongbao.Text = "<img src='../../Images/icon/Icon_warning.gif' alt='Cảnh báo' /> <span style='font-size:11px;color:red;'>  Không có danh mục này </span>"; return; }
        hidID.Value = ID.ToString();
        txtTieude.Text = Server.HtmlDecode(_TblTinTucEntity.TieuDe);
        txtTomTat.Text = Server.HtmlDecode(_TblTinTucEntity.TomTat);
        fckMoTa.Value = Server.HtmlDecode(_TblTinTucEntity.Mota);
        ddlCateParent.SelectedValue = _TblTinTucEntity.DanhMucTinId.ToString();
        ddlLoaiTin.SelectedValue = _TblTinTucEntity.NhomTinId.ToString();
        img_news.Visible = true;
        img_news.ImageUrl = (_TblTinTucEntity.Image != "") ? Server.HtmlDecode(_TblTinTucEntity.Image) : "~/AdminPortal/Images/icon/no_image.jpg";
        if (string.IsNullOrEmpty(_TblTinTucEntity.Image))
        {
            imgbtn_Delete.Enabled = false;
            imgbtn_Delete.ImageUrl = "~/AdminPortal/images/icon/remove_16_hiden.gif";
            img_news.Visible = false;
        }
        else
        {
            img_news.Visible = true;
        }
        chkNoiBat.Checked = Convert.ToBoolean(_TblTinTucEntity.TinNoiBat);
        chkStatus.Checked = Convert.ToBoolean(_TblTinTucEntity.TrangThai);
        chkFiles.Checked = Convert.ToBoolean(_TblTinTucEntity.KieuTin);
        chkFiles_CheckedChanged(new object(), new EventArgs());
        if (!string.IsNullOrEmpty(_TblTinTucEntity.Image))
            FileUpload1.FilePath = _TblTinTucEntity.Image;
    }

    private void loadCateParent(DropDownList _ddt,string _type)
    {
        TblDanhMucTinEntity _TblDanhMucTinEntity = new TblDanhMucTinEntity();
        _TblDanhMucTinEntity.LangId = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
        _TblDanhMucTinEntity.Type = _type;
        _TblDanhMucTinEntity.SortDirection = "ASC";
        DataTable _dtDM = new TblDanhMucTinManager().be_SelectToDropdownlist(_TblDanhMucTinEntity);
        if (_dtDM != null || _dtDM.Rows.Count > 0)
            HNG.LIB.BIND_SOURCE_V3.Bind_DropDownList(_ddt, _dtDM, TblDanhMucTinFields.TenDanhMuc.Name, TblDanhMucTinFields.Id.Name, TblDanhMucTinFields.DanhMucCha.Name, true);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        BindToData();
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            TblTinTucEntity _TblTinTucEntity = _TblTinTucManager.SelectOne(Convert.ToInt32(hidID.Value));
            if (_TblTinTucEntity != null)
            {
                if (!string.IsNullOrEmpty(_TblTinTucEntity.Image))
                {
                    string ImageDefault = Server.MapPath(_TblTinTucEntity.Image);
                    try { File.Delete(ImageDefault); }
                    catch { }
                }
            }
            _TblTinTucManager.Delete(Convert.ToInt32(hidID.Value));
        }
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnApply_Click(object sender, EventArgs e)
    {
        if (txtTieude.Text == "") { lblthongbao.Text = "<img src='../Images/icon/Icon_warning.gif' alt='Cảnh báo' />  <span style='font-size:11px;color:red;'> Nhập tiêu đề tin !</span>"; txtTieude.Focus(); return; }
        BindToData();
        //Response.Redirect(ResolveUrl("~/AdminPortal/Pages/NewsEdit.aspx?id=") + hidID.Value);
        BindData(Convert.ToInt32(hidID.Value));
    }

    private void BindToData()
    {        
        TblTinTucEntity _TblTintucEntity = new TblTinTucEntity();
        if (!string.IsNullOrEmpty(hidID.Value))
            _TblTintucEntity.Id = Convert.ToInt32(hidID.Value);
        else
        {
            _TblTintucEntity.NgayDang = DateTime.Now;
            _TblTintucEntity.TruyCap = 1;
        }
        _TblTintucEntity.DanhMucTinId = Convert.ToInt32(ddlCateParent.SelectedValue);
        _TblTintucEntity.NhomTinId = Convert.ToInt32(ddlLoaiTin.SelectedValue);
        //_TblTintucEntity.LangId = ddlLang.SelectedValue;        
        _TblTintucEntity.TieuDe =Server.HtmlEncode( txtTieude.Text);
        _TblTintucEntity.TomTat =Server.HtmlEncode( txtTomTat.Text);
        _TblTintucEntity.Mota = Server.HtmlEncode(fckMoTa.Value);
        _TblTintucEntity.TenDanhMuc = ddlCateParent.SelectedItem.Text;
        _TblTintucEntity.LangId = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
        _TblTintucEntity.TenLoaiDanhMuc = ddlLoaiTin.SelectedItem.Text;
        _TblTintucEntity.StrKieuTin = "TXT";
        if (FileUpload1.FilePath != "")
        {
            if (chkFiles.Checked)
            {
                _TblTintucEntity.StrKieuTin = Path.GetExtension(Server.MapPath(FileUpload1.FilePath)).Substring(1).ToUpper();
            }
            if (FileUpload1.FilePath != _TblTintucEntity.Image)
            {                
                string ImageDefault = Server.MapPath(_TblTintucEntity.Image);
                try { File.Delete(ImageDefault); }
                catch { }
                _TblTintucEntity.Image = Server.HtmlEncode(FileUpload1.FilePath);
            }
        }
        _TblTintucEntity.TrangThai = (chkStatus.Checked == true) ? true : false;
        _TblTintucEntity.TinNoiBat = (chkNoiBat.Checked == true) ? true : false;
        _TblTintucEntity.KieuTin = (chkFiles.Checked == true) ? true : false;
        
        if (!string.IsNullOrEmpty(hidID.Value))
            _TblTinTucManager.Update(_TblTintucEntity);
        else
        {
            _TblTinTucManager.Insert(_TblTintucEntity);
            hidID.Value = _TblTintucEntity.Id.ToString();
        }

    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void imgbtn_Delete_Click(object sender, ImageClickEventArgs e)
    {
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            TblTinTucEntity _TblTinTucEntity = _TblTinTucManager.SelectOne(Convert.ToInt32(hidID.Value));
            if (_TblTinTucEntity != null)
            {
                if (!string.IsNullOrEmpty(_TblTinTucEntity.Image))
                {
                    string defaultImage = Server.MapPath(_TblTinTucEntity.Image);
                    if (File.Exists(defaultImage)) File.Delete(defaultImage);
                    _TblTinTucEntity.Image = "";
                    _TblTinTucManager.Update(_TblTinTucEntity);
                }
            }
        }
        BindData(Convert.ToInt32(hidID.Value));
    }
    protected void chkFiles_CheckedChanged(object sender, EventArgs e)
    {
        if (chkFiles.Checked)
        {
            ShowChonFile.Visible = true;
            ShowNoiDung.Visible = false;
        }
        else
        {
            ShowChonFile.Visible = false;
            ShowNoiDung.Visible = true;
        }
    }
}